package persistence.impl;

import domain.Cart;
import domain.Product;
import persistence.CartDAO;
import persistence.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class CartDAOImpl implements CartDAO {
    private static final String GET_CART_BY_USER_ID =
            "SELECT * FROM cart WHERE user_id=?";
    private static final String ADD_CART =
            "INSERT INTO `cart` (product_id,user_id,img_src,unit_price,price,num,name) VALUES (?,?,?,?,?,?,?)";
    private static final String DELETE_CART_BY_CART_ID =
            "DELETE FROM cart WHERE cart_id IN ";

    @Override
    public List<Cart> getCartByUserid(String userid) {
        List<Cart> cartList = new ArrayList<>();
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_CART_BY_USER_ID);
            preparedStatement.setString(1, userid);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                cartList.add(toCart(resultSet));
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return cartList;
    }

    @Override
    public boolean addCart(Cart cart) {
        boolean result = false;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(ADD_CART);
            preparedStatement.setString(1,cart.getProductid());
            preparedStatement.setString(2,cart.getUserid());
            preparedStatement.setString(3,cart.getImageSrc());
            preparedStatement.setBigDecimal(4,cart.getUnitprice());
            preparedStatement.setBigDecimal(5,cart.getPrice());
            preparedStatement.setInt(6,cart.getNum());
            preparedStatement.setString(7,cart.getName());
            int row = preparedStatement.executeUpdate();
            if(row == 1){
                result = true;
            }
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public boolean deleteCartByCartid(List<Integer> cartid) {
        boolean result = false;
        try {
            StringBuilder delete = new StringBuilder(DELETE_CART_BY_CART_ID + " (");
            for(int i = 0;i < cartid.size();i++){
                delete.append(cartid.get(i));
                if(i + 1 < cartid.size()){
                    delete.append(",");
                }
            }
            delete.append(")");
            Connection connection = DBUtil.getConnection();
            Statement statement = connection.createStatement();
            int row = statement.executeUpdate(delete.toString());
            if(row == cartid.size()){
                result = true;
            }
            DBUtil.closeStatement(statement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    private Cart toCart(ResultSet resultSet) {
        Cart cart = new Cart();
        try {
            cart.setNum(resultSet.getInt("num"));
            cart.setImageSrc(resultSet.getString("img_src"));
            cart.setPrice(resultSet.getBigDecimal("price"));
            cart.setUnitprice(resultSet.getBigDecimal("unit_price"));
            cart.setProductid(resultSet.getString("product_id"));
            cart.setUserid(resultSet.getString("user_id"));
            cart.setName(resultSet.getString("name"));
            cart.setCartid(resultSet.getInt("cart_id"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return cart;
    }
}
